package com.miniot.base.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miniot.base.dto.TypeTwoTreeDTO;
import com.miniot.base.entity.TypeOne;
import com.miniot.base.entity.TypeTwo;
import com.miniot.base.mapper.TypeOneMapper;
import com.miniot.base.service.TypeOneService;
import com.miniot.base.service.TypeTwoService;
import com.miniot.base.vo.TypeOnePageVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 一级类型表
 *
 * @author zzq
 * @date 2023-10-17 11:16:02
 */
@Service
@AllArgsConstructor
@Slf4j
public class TypeOneServiceImpl extends ServiceImpl<TypeOneMapper, TypeOne> implements TypeOneService {

    private final TypeOneMapper mapper;
    private final TypeTwoService typeTwoService;
    private final TypeOneMapper typeOneMapper;
    @Override
    @Transactional
    public boolean removeTypeOneById(Long id) {
        Integer typeOne = mapper.getTypeOne(id);
        if (typeOne!=0){
            throw new RuntimeException("该类型正在使用,不可删除");
        }
        this.removeById(id);
        typeTwoService.remove(new LambdaQueryWrapper<TypeTwo>().eq(TypeTwo::getTypeOneId,id));
        return true;
    }

    @Override
    public Page<TypeOnePageVO> pageChild(Page<TypeOne> page, String typeOneName) {
        LambdaQueryWrapper<TypeOne> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(typeOneName),TypeOne::getTypeOneName,typeOneName);
        Page<TypeOne> typeOnePage = typeOneMapper.selectPage(page, wrapper);
        List<TypeOnePageVO> data = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(typeOnePage.getRecords().size())) {
            typeOnePage.getRecords().forEach(m->{
                List<TypeTwo> list1 = typeTwoService.list(new LambdaQueryWrapper<TypeTwo>().eq(TypeTwo::getTypeOneId, m.getId()));
                List<TypeOnePageVO> child = new ArrayList<>();
                list1.forEach(l->{
                    child.add(TypeOnePageVO.builder()
                            .typeCode(l.getTypeTwoCode())
                            .id(l.getId())
                            .typeName(l.getTypeTwoName())
                            .build());
                });
                data.add(TypeOnePageVO.builder()
                        .typeCode(m.getTypeOneCode())
                        .id(m.getId())
                        .typeName(m.getTypeOneName())
                        .child(child)
                        .build());

            });
        }
        Page<TypeOnePageVO> result = new Page<>();
        result.setRecords(data);
        result.setSize(page.getSize());
        result.setCurrent(page.getCurrent());
        result.setTotal(page.getTotal());

        return result;
    }

}
